package cn.pxs.service;

import cn.pxs.entity.BookType;
import cn.pxs.mapper.BookTypeMapper;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//书本类型业务接口实现类
@Service
public class BookTypeServiceImpl implements BookTypeService {
    @Autowired
    private BookTypeMapper bookTypeMapper;

    @Override
    public List<BookType> findAll() {//单层类
        List<BookType> books = bookTypeMapper.findAll();
        return books;
    }

    @Override
    //双层的findAll
    public List<BookType> findAll2() {
        return bookTypeMapper.findAll2();
    }

    @Override
    //根据 id 查询 （更新了为93版）
    public BookType findById(Integer id) {
        BookType type = bookTypeMapper.findById(id);
        return type;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加
    public void add(BookType bookType) {
        bookTypeMapper.add(bookType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加 或 修改  92版
    public List<BookType> updateOrAdd(BookType bookType) {
        if(bookType.getId() == null || bookType.getId()==0){
            bookTypeMapper.add(bookType);
        }
        if(bookType.getId() != null || bookType.getId()!=0){
            bookTypeMapper.update(bookType);
        }
       return bookTypeMapper.findAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加 或 修改 93版
    public List<BookType> updateOrAdd2(BookType bookType) {
        if(bookType.getId() == null || bookType.getId()==0){
            bookTypeMapper.add(bookType);
        }
        if(bookType.getId() != null || bookType.getId()!=0){
            bookTypeMapper.update(bookType);
        }
        return bookTypeMapper.findAll2();
    }
}
